#include "q_shared.h"
#include "bg_gamex86funcs.h"
#include "bg_global.h"
#include <math.h>
#include <stdlib.h>

void SP_misc_model_gun_rack2( gentity_t *ent )
{
	int guncount = 0;
	int i;
	gitem_t *wp_blaster;
	gitem_t *wp_repeater;
	gitem_t *wp_rocket;
	gitem_t *wp_flechette;
	gitem_t *wp_concussion;
	gitem_t *wp_disruptor;
	gitem_t *guns[3];
	vec3_t positioning;
	qboolean hasBlaster = qfalse;
	qboolean hasRepeater = qfalse;
	qboolean hasRocket = qfalse;
	qboolean hasFlechette = qfalse;
	qboolean hasConcussion = qfalse;
	qboolean hasDisruptor = qfalse;

	if(ent->spawnflags & 1)
	{
		wp_blaster = BG_FindItemForWeapon(WP_BLASTER);
		hasBlaster = qtrue;
	}
	if(ent->spawnflags & 2)
	{
		wp_repeater = BG_FindItemForWeapon(WP_REPEATER);
		hasRepeater = qtrue;
	}
	if(ent->spawnflags & 4)
	{
		wp_rocket = BG_FindItemForWeapon(WP_ROCKET_LAUNCHER);
		hasRocket = qtrue;
	}
	if(ent->spawnflags & 8)
	{
		wp_flechette = BG_FindItemForWeapon(WP_FLECHETTE);
		hasFlechette = qtrue;
	}
	if(ent->spawnflags & 16)
	{
		wp_concussion = BG_FindItemForWeapon(WP_CONCUSSION);
		hasConcussion = qtrue;
	}
	if(ent->spawnflags & 32)
	{
		wp_disruptor = BG_FindItemForWeapon(WP_DISRUPTOR);
		hasDisruptor = qtrue;
	}

	if(!hasBlaster && !hasRepeater && !hasRocket && !hasFlechette && !hasConcussion && !hasDisruptor)
	{
		hasBlaster = qtrue;
		wp_blaster = BG_FindItemForWeapon(WP_BLASTER);
	}

	if(hasBlaster)
	{
		positioning[guncount] = 23.0f;		//dunno if this is correct
		guns[guncount++] = wp_blaster;
	}
	if(hasRepeater)
	{
		positioning[guncount] = 24.5f;		//dunno if this is correct
		guns[guncount++] = wp_repeater;
	}
	if(hasRocket)
	{
		positioning[guncount] = 25.5f;
		guns[guncount++] = wp_rocket;
	}
	if(hasFlechette)
	{
		if(guncount >= 3)
		{
			positioning[guncount-3] = 25.5f;
			guns[guncount-3] = wp_flechette;
			guncount++;
		}
		else
		{
			positioning[guncount] = 25.5f;
			guns[guncount++] = wp_flechette;
		}
	}
	if(hasConcussion)
	{
		if(guncount >= 3)
		{
			positioning[guncount-3] = 25.5f;
			guns[guncount-3] = wp_concussion;
			guncount++;
		}
		else
		{
			positioning[guncount] = 25.5f;
			guns[guncount++] = wp_concussion;
		}
	}
	if(hasDisruptor)
	{
		if(guncount >= 3)
		{
			positioning[guncount-3] = 24.5f;
			guns[guncount-3] = wp_disruptor;
			guncount++;
		}
		else
		{
			positioning[guncount] = 24.5f;
			guns[guncount++] = wp_disruptor;
		}
	}
	if(guncount > 0)
	{
		int xPos = -9;
		for( ; guncount < 3; guncount++)
		{
			//Fill in blank spots
			positioning[guncount] = positioning[0];
			guns[guncount] = guns[0];
		}
		for(i = 0; i < guncount; i++)
		{
			//Don't question this math, it just works (for some reason)
			//TODO: clean up this nastynastynasty mess.
			float randX = (float)rand();
			float randY = (float)rand();
			float gunAngle = ((randX/32768) - 0.5 + (randX/32768) - 0.5);
			float gunAngle2 = ((randY/32768) - 0.5 + (randY/32768) - 0.5);
			float gunAngleFinal = gunAngle + gunAngle + xPos;
			float gunAngleFinal2 = gunAngle2 + gunAngle2;
			G_Unknown1(guns[i], ent->origin, ent->angles, gunAngleFinal2, gunAngleFinal, positioning[i]);
			xPos += 9;
		}
	}
	ent->modelIndex = G_ModelIndex("models/map_objects/kejim/weaponsrack.md3");
	G_SetOrigin(ent, ent->origin);
	G_SetAngles(ent, ent->angles);
	ent->contents = 1; //CONTENTS_SOLID;
	gi->linkentity(ent);
}